package com.ahwei.web.listener;

import com.ahwei.dao.function.FunctionDao;
import com.ahwei.pojo.Function;
import com.ahwei.util.LogUtils;
import com.ahwei.util.MyBatisUtils;
import com.ahwei.util.ScanUtil;
import com.ahwei.util.anno.Controller;
import com.ahwei.util.anno.FunctionMapping;
import com.ahwei.util.anno.RequestMapping;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-05 18:39:12
 * @description: 这是一个Java文件！
 */
@WebListener
public class SystemListener implements ServletContextListener,HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public SystemListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            LogUtils.info("Listener启动...");
            SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
            FunctionDao functionDao = sqlSession.getMapper(FunctionDao.class);
            //查询数据库获取功能列表的 funUrl 字段
            List<String> funUrlList = functionDao.queryAllFunUrl();

            //扫描 com.目录，获取有 Controller 注解的类的集合
            Set<Class> classes = ScanUtil.scanPackage4AnnotatedClass("com.ahwei.controller", Controller.class);

            //遍历获取到的class集合
            int i=0;
            for (Class clazz : classes){
                //遍历类中的方法集合，创建url与方法的映射关系
                Method[] methods = clazz.getMethods();
                for(Method method : methods){
                    //获取方法上的RequestMapping,Function注解
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    FunctionMapping functionMapping = method.getAnnotation(FunctionMapping.class);
                    //如果RequestMapping注解存在，创建url映射目录
                    Function function = new Function();
                    String funUrl,funName;
                    if (requestMapping != null) {
                        funUrl = requestMapping.value();
                        //检查此url是否已在数据库中存在
                        if(!funUrlList.contains(funUrl)){
                            function.setFunUrl(funUrl);
                            if (functionMapping != null){
                                funName = functionMapping.value();
                                function.setFunName(funName);
                                function.setStart(1);
                                //添加新功能
                                functionDao.add(function);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attribute
         is replaced in a session.
      */
    }
}
